package petstore.web.servlet.item;

import petstore.persistence.CommentDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/commentEdit")
public class CommentEditServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String comment = req.getParameter("comment");
        String itemName = req.getParameter("itemName");
        HttpSession session = req.getSession();
        String username = (String)session.getAttribute("username");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/plain");
        if(username == null)
        {
            resp.getWriter().print(0);
            return;
        }
        CommentDaoImpl commentDao = new CommentDaoImpl();
        if(commentDao.deleteCommentByUsernameAndItemnameAndComment(username,comment,itemName))
        {
            resp.getWriter().print(1);
        }
        else
        {
            resp.getWriter().print(0);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        this.doGet(req, resp);
    }
}
